package cn.gov.msa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Consulting;
import cn.gov.msa.service.ConsultingService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/consulting", produces="text/plain;charset=UTF-8")
public class ConsultingController {

	@Autowired
	private ConsultingService consultingService;
	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params) throws Exception {
		
		ObjectMapper mapper = new ObjectMapper(); 	
		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		

		Consulting consulting = mapper.readValue(params.toString(), Consulting.class);
		
		// 权限控制 只能搜索当前登录账号所属机构以及下级机构数据
		if(consulting.getOrgCode() == null || consulting.getOrgCode().equals("")){
			consulting.setOrgCode(consulting.getSysorg());
		}
		
		List<Consulting> consultingList = consultingService.selectRepository(pageNum, pageSize, consulting);
		
	    //获取分页信息
        PageInfo<Consulting> page = new PageInfo<Consulting>(consultingList);
        

        JSONArray ja = JSONArray.fromObject(consultingList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
 
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/save")
	public String save(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
	    ObjectMapper mapper = new ObjectMapper(); 
		
	    try{
			
			//得到类型的model数据
	    	Consulting consulting = mapper.readValue(param.toString(), Consulting.class);	
		    
		    save = consultingService.saveData(consulting);
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = "数据保存异常！";
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
	    ObjectMapper mapper = new ObjectMapper(); 	
		
	    try{
			
	    	Consulting consulting = mapper.readValue(param.toString(), Consulting.class);	
	    	System.out.println(consulting);
		    save = consultingService.updateData(consulting);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = "数据保存异常！";
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	
}
